package domain;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;

import database.access.layer.MovieDal;
import database.access.layer.PreferenceDal;
import database.access.layer.UserDal;


public class User {
	private String name;
	private String surname;
	private int userId;
	private String email;
	private String password;
	private Preference preference;
	private List<String> ratedMovies;

	public User(String name, String surname, int userId, String email, String password) {
		this.name = name;
		this.userId = userId;
		this.setSurname(surname);
		this.setEmail(email);
		this.setPassword(password);
		this.ratedMovies = new ArrayList<String>();
	}
	public User(){
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) throws Exception {

		if (name == null) {
			throw (new Exception("Name is incorrect!"));
		} else {
			this.name = name;
		}
	}
	
	public List<String> getRatedMovies() {
		return ratedMovies;
	}
	public void setRatedMovies(List<String> ratedMovies) {
		this.ratedMovies = ratedMovies;
	}

	public Preference getPreference() {
		return preference;
	}

	public void setPreference(Preference preference) {
		this.preference = preference;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) throws Exception {

		if (userId < 0) {
			throw (new Exception("UserId is incorrent!"));
		} else {
			this.userId = userId;
		}
	}

	public void rateMovie(int movieId, int value) throws SQLException {
		this.preference = new Preference(this.userId, movieId, value);
		PreferenceDal preferenceDal = new PreferenceDal();
		//MovieDal movieDal = new MovieDal();
		preferenceDal.savePreference(preference);
		//ratedMovies.add(movieDal.findMovieById(movieId));
		
	}
	
	public List<RecommendedItem> getRecommend (int howManyRecommendation) throws TasteException{
		Recommendation recommendation = new Recommendation();
		List<RecommendedItem> recommendations = recommendation.recommend(this.userId, howManyRecommendation);
		return recommendations;
	}
	
	public static User userLogIn(String email, String password) throws SQLException{
		UserDal userDal = new UserDal();
		return userDal.getUser(email, password);
	}
	
	public static void registerUser(User user) throws Exception{
		UserDal userDal = new UserDal();
		userDal.saveUser(user);
	}
	
	public ArrayList<String> getUserRatedMovies() throws SQLException{
		UserDal userDal = new UserDal();
		return userDal.getLastedRatedMovies(userId);
	}
	

	@Override
	public String toString() {
		return "User [name=" + name + ", userId=" + userId + "]";
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


}
